package top.malaoshi.webmvc.info;

import top.malaoshi.webmvc.ResponseHandler;
import top.malaoshi.webmvc.parameter.RequestParamAndMethodArgsResolver;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * 封装方法的相关信息
 */
public class MethodInfo {

    private ClassInfo classInfo;
    Method method;
    /**
     * 该方法所属 class 的 bean名字
     */
    String beanName;

    /**
     * 该方法的响应处理器
     */
    ResponseHandler responseHandler;

    /**
     * 封装 RequestMapping、GetMapping 等注解信息
     */
    RequestMappingInfo requestMappingInfo;

    /**
     * 封装 @Bean 注解信息
     */
    BeanAnnotationInfo beanAnnotationInfo;

    /**
     * 该方法每一个参数的解析器
     */
    List<RequestParamAndMethodArgsResolver> argumentResolvers = new ArrayList<>();

    List<MethodsArgumentInfo> methodsArgumentInfos = new ArrayList<>();

    public MethodInfo(Method method) {
        this.method = method;
    }

    public MethodInfo(String beanName , Method method, ResponseHandler responseHandler, RequestMappingInfo requestMappingInfo) {
        this.method = method;
        this.beanName = beanName;
        this.responseHandler = responseHandler;
        this.requestMappingInfo = requestMappingInfo;
    }

    public static MethodInfo of(String beanName , Method method,ResponseHandler responseHandler,RequestMappingInfo requestMappingInfo){
        return new MethodInfo(beanName,method,responseHandler,requestMappingInfo);
    }

    /**
     * 添加该方法形参的信息
     * @param mai
     */
    public void addMethodsArgumentInfo(MethodsArgumentInfo mai){
        methodsArgumentInfos.add(mai);
    }

    /**
     * 添加该方法形参的解析器
     * 接收请求时，从请求中解析出参数
     */
    public void addArgumentResolvers(RequestParamAndMethodArgsResolver resolver){
        argumentResolvers.add(resolver);
    }

    public List<RequestParamAndMethodArgsResolver> getArgumentResolvers() {
        return argumentResolvers;
    }

    public List<MethodsArgumentInfo> getMethodsArgumentInfos() {
        return methodsArgumentInfos;
    }

    public Method getMethod() {
        return method;
    }

    public String getBeanName() {
        return beanName;
    }

    public RequestMappingInfo getRequestMappingInfo() {
        return requestMappingInfo;
    }

    public ResponseHandler getResponseHandler() {
        return responseHandler;
    }

    public BeanAnnotationInfo getBeanAnnotationInfo() {
        return beanAnnotationInfo;
    }

    public void setBeanAnnotationInfo(BeanAnnotationInfo beanAnnotationInfo) {
        this.beanAnnotationInfo = beanAnnotationInfo;
    }

    public void setClassInfo(ClassInfo classInfo) {
        this.classInfo = classInfo;
    }
}
